home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Creator Changer 1.0.3 / Code and Resource / Creator Changer.change.c next >
Encoding:
C/C++ Source or Header  |  1994-11-14  |  5.7 KB  |  217 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the bulf of the program.  The functions which 
  3.  *    are invloved in the changing of the creator and file types are in
  4.  *    this file.
  5.  **********************************************************************/
  6.  
  7. #include "Creator Changer.h"
  8. #include "Creator Changer.change.h"
  9.  
  10.  
  11.  
  12. /**********************************************************************
  13.  *    Function Open_Changer_DLOG, this function gets the info from the
  14.  *    Changer Dialog box.
  15.  **********************************************************************/
  16.  
  17. void Open_Changer_DLOG(void)
  18.     {
  19.     
  20.     Handle        item_handle;
  21.     short        the_item;
  22.     short        item_type;
  23.     Rect        item_rect;
  24.     Boolean        done_with_dialog=FALSE;
  25.     GrafPtr        old_port;
  26.  
  27.     The_Dialog=GetNewDialog(CHNG_TYPE_DLOG_ID, NIL_PTR, IN_FRONT);
  28.     ShowWindow(The_Dialog);
  29.     GetPort(&old_port);
  30.     SetPort(The_Dialog);
  31.  
  32.     while(!done_with_dialog)
  33.         {
  34.         Update_Change_Dialog();
  35.         ModalDialog(NIL_PTR, &the_item);
  36.         switch(the_item)
  37.             {
  38.             case CHNG_OK:
  39.                     //    if the change is GOOD, then quit the dialog box
  40.                 done_with_dialog=Handle_OK_Change();
  41.                 break;
  42.             case CHNG_CANCEL:
  43.                 done_with_dialog=TRUE;
  44.                 break;
  45.             defualt:
  46.                 break;
  47.               }
  48.           }
  49.  
  50.     DisposDialog(The_Dialog);
  51.     SetPort(old_port);
  52.  
  53.     }
  54.  
  55.  
  56.  
  57. /**********************************************************************
  58.  *    Function Update_Change_Dialog, this function updates the change
  59.  *    dialog incase the dialog is covered by another window.
  60.  **********************************************************************/
  61.  
  62. void Update_Change_Dialog(void)
  63.     {
  64.     
  65.     Handle        handle;
  66.     short        diameter;
  67.     short        type;
  68.     Rect        rect;
  69.     
  70.         //    Draw the current creator/file type text boxes
  71.     Draw_Dialog();
  72.     
  73.         //    Put a border around the OK button
  74.     GetDItem(The_Dialog, CHNG_OK, &type, &handle, &rect);
  75.     diameter=(rect.bottom-rect.top)/2+6;
  76.     if(diameter<16) diameter=16;
  77.     PenSize(3, 3);
  78.     InsetRect(&rect, -4, -4);
  79.     FrameRoundRect(&rect, diameter, diameter);
  80.     
  81.     }
  82.  
  83.  
  84.  
  85. /**********************************************************************
  86.  *    Function Draw_Dialog, this function draws the objects in the
  87.  *    main dialog box.
  88.  **********************************************************************/
  89.  
  90. void Draw_Dialog(void)
  91.     {
  92.     
  93.     Rect        the_rect;
  94.     File_Union    temp, type;
  95.     int            i;
  96.     
  97.     TextFont(systemFont);
  98.     TextSize(12);
  99.     TextFace(0);
  100.     PenSize(1, 1);
  101.     SetRect(&the_rect, 115, 55, 196, 77);
  102.     FrameRect(&the_rect);
  103.     SetRect(&the_rect, 236, 55, 317, 77);
  104.     FrameRect(&the_rect);
  105.     
  106.     type.TEXT[0]=TYPE_LEN;
  107.     
  108.     MoveTo(119, 70);
  109.     
  110.         //    This is where I convert the types from an int to a char:
  111.         //     • First, I Make a file union with an unsigned int and a char string
  112.         //     • Then, I make a temp variable, where the unsinged int is assigned the type
  113.         //     • Next, I use a loop to copy the temp.TEXT to the variable type.TEXT, offset by one
  114.         //     • Last, I draw the type.TEXT string
  115.     temp.LONG=File_Info.fdCreator;
  116.     for(i=0;i<=TYPE_LEN;i++) type.TEXT[i+1]=temp.TEXT[i];
  117.     DrawString(type.TEXT);
  118.     
  119.     MoveTo(240, 70);
  120.     temp.LONG=File_Info.fdType;
  121.     for(i=0;i<=TYPE_LEN;i++) type.TEXT[i+1]=temp.TEXT[i];
  122.     DrawString(type.TEXT);
  123.         
  124.     }
  125.  
  126.  
  127.  
  128. /**********************************************************************
  129.  *    Function Handle_OK_Change, this function calls the functions
  130.  *    which do the changing of the creator and file types.  However, 
  131.  *    if there is no information entered in the text fields the function
  132.  *    will alert the user.
  133.  **********************************************************************/
  134.  
  135. int Handle_OK_Change(void)
  136.      {
  137.      
  138.          //    Check to see if there is good data in the edit fields ...
  139.     if(Check_Type_Data(CHNG_FILE) && Check_Type_Data(CHNG_CREATOR))
  140.         {
  141.         File_Info.fdType=Set_Type(CHNG_FILE);
  142.         File_Info.fdCreator=Set_Type(CHNG_CREATOR);
  143.         
  144.             //    Set the new creator and file types
  145.         SetFInfo(File_Name, V_Ref_Num, &File_Info);
  146.         
  147.             //    We sucessfully completed our mission !!
  148.         return(GOOD);
  149.         }
  150.         
  151.         //    ... if not, the change was BAD.
  152.     else
  153.         {
  154.         PREV_ALERT=FALSE;    //    Reset the alert.
  155.         return(BAD);
  156.         }
  157.     }
  158.  
  159.  
  160.  
  161. /**********************************************************************
  162.  *    Function Set_File_Type, this function sets the file type of the
  163.  *    specified file.  (converts a string in a edit field to an 
  164.  *    unsigned decimal equivalent).
  165.  **********************************************************************/
  166.  
  167. OSType Set_Type(int ID)
  168.     {
  169.     
  170.     File_Union    file_type, temp;
  171.     Handle        item_handle;
  172.     short        item_type;
  173.     Rect        item_rect;
  174.     int            i;
  175.     
  176.     GetDItem(The_Dialog, ID, &item_type, &item_handle, &item_rect);
  177.     GetIText(item_handle, temp.TEXT);
  178.     
  179.         //    This is just the oposite as I did above:
  180.     for(i=0;i<=TYPE_LEN;i++) file_type.TEXT[i]=temp.TEXT[i+1];
  181.     return(file_type.LONG);
  182.     
  183.     }
  184.  
  185.  
  186.  
  187. /**********************************************************************
  188.  *    Function Check_Type_Data, this function checks the type entered
  189.  *    by the user.  If there are more than 4 charachters than there is 
  190.  *    an error, and if there is no data entered in there is an error.
  191.  **********************************************************************/
  192.  
  193. int Check_Type_Data(int ID)
  194.     {
  195.     
  196.     Str15        item_text;        //    Allow a buffer incase someone trys to type in ...
  197.     Handle        item_handle;    //    ... more than four characters.
  198.     short        item_type;
  199.     Rect        item_rect;
  200.     short        len;
  201.     
  202.     GetDItem(The_Dialog, ID, &item_type, &item_handle, &item_rect);
  203.     GetIText(item_handle, item_text);
  204.     
  205.         //    see if the length of the string is zero (no data) or if it too long (over 4)...
  206.         //    ... and see if the alert was given before (don't want to annoy the user).
  207.     len=item_text[0];
  208.     if(len!=TYPE_LEN && !PREV_ALERT)
  209.         {
  210.         ParamText("\pTypes must be four (4) characters long!","\p", "\p(Spaces included)", "\p");
  211.         Alert(ERROR_ALERT, NIL_PTR);
  212.         PREV_ALERT=TRUE;    //    The alert was signaled once.  Don't need to alert ...
  213.         return(BAD);        //    ... someone twice in a row!
  214.         }
  215.     else return(GOOD);
  216.     
  217.     }